﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.Xml;
using ChampionRecomendedItems.Mappers;

namespace ChampionRecomendedItems.UI
{
    public partial class EscolhaItems : Form
    {
        private List<Item> _listaItemsCompleto = null;
        private List<Item> _listaItemSelecionado = null;


        public EscolhaItems()
        {
            InitializeComponent();
            this._listaItemsCompleto = new List<Item>();
            _listaItemSelecionado = new List<Item>();
        }

        private void EscolhaItems_Load(object sender, EventArgs e)
        {
            this.CarregarListaItems();
            this.CarregarList();
            this.VerificarQuantidadeMaximaPermitida();
        }

        private void CarregarList()
        {
            this.lstItemTotal.DisplayMember = "Nome";
            this.lstItemTotal.ValueMember = "Id";
            this.lstItemTotal.DataSource = this.MapperItemToDataTable(this._listaItemsCompleto, true);
        }

        private List<Item> CarregarListaItems()
        {
            XmlDocument documentoXml = new XmlDocument();
            documentoXml.Load(@"C:\ExemplosConsole\CustomizacaoChamp\items.xml");
            List<Item> listaItems = new List<Item>();
            Item objItem = new Item();
            int quantidadeNos = documentoXml.ChildNodes.Item(1).ChildNodes.Count;
            for (int i = 0; i < quantidadeNos; i++)
            {
                objItem = new Item();
                objItem.Id = Convert.ToInt32(documentoXml.ChildNodes.Item(1).ChildNodes.Item(i).ChildNodes.Item(0).InnerText);
                objItem.Nome = documentoXml.ChildNodes.Item(1).ChildNodes.Item(i).ChildNodes.Item(1).InnerText;
                listaItems.Add(objItem);
            }
            this._listaItemsCompleto = listaItems;
            return listaItems;
        }

        private DataTable MapperItemToDataTable(List<Item> items, bool total)
        {
            DataTable retorno = new DataTable();
            DataRow linha = null;

            retorno.Columns.Add("Id");
            retorno.Columns.Add("Nome");


            if (total)
            {
                if (this.lstItemSelecionado.Items.Count > 0)
                {
                    var query = (from i in this._listaItemsCompleto
                                 where !(from i1 in this._listaItemSelecionado
                                         select i1.Id).Contains(i.Id)
                                 select i).ToList();
                    items = query;
                }
            }

            foreach (Item objItem in items)
            {
                linha = retorno.NewRow();
                linha["Id"] = objItem.Id;
                linha["Nome"] = objItem.Nome;
                retorno.Rows.Add(linha);
            }

            return retorno;
        }

        private void VerificarQuantidadeMaximaPermitida()
        {
            if (this.lstItemSelecionado.Items.Count == 6)
            {
                this.btnAdicionar.Enabled = false;
                this.btnOk.Enabled = true;
            }
            else
            {
                if (this.lstItemSelecionado.Items.Count != 0)
                    this.btnRemover.Enabled = true;
                else
                    this.btnRemover.Enabled = false;
                this.btnAdicionar.Enabled = true;
                this.btnOk.Enabled = false;
            }
        }

        private void btnAdicionar_Click(object sender, EventArgs e)
        {
            Item objItem = new Item();
            objItem.Id = Convert.ToInt32(this.lstItemTotal.SelectedValue);
            objItem.Nome = ((DataRowView)this.lstItemTotal.SelectedItem).Row.ItemArray[1].ToString();
            this._listaItemSelecionado.Add(objItem);
            this.lstItemSelecionado.DisplayMember = "Nome";
            this.lstItemSelecionado.ValueMember = "Id";
            this.lstItemSelecionado.DataSource = this.MapperItemToDataTable(this._listaItemSelecionado, false);
            this.lstItemTotal.DataSource = this.MapperItemToDataTable(this._listaItemsCompleto, true);
            VerificarQuantidadeMaximaPermitida();
        }

        private void btnRemover_Click(object sender, EventArgs e)
        {
            var query = (from i in this._listaItemSelecionado
                         where i.Id != Convert.ToInt32(this.lstItemSelecionado.SelectedValue)
                         select i).ToList();
            this._listaItemSelecionado = query;
            this.lstItemSelecionado.DataSource = this.MapperItemToDataTable(this._listaItemSelecionado, false);
            this.lstItemTotal.DataSource = this.MapperItemToDataTable(this._listaItemsCompleto, true);
            VerificarQuantidadeMaximaPermitida();
        }

        private void btnLimpar_Click(object sender, EventArgs e)
        {
            this._listaItemSelecionado.Clear();
            this.lstItemSelecionado.DataSource = this.MapperItemToDataTable(this._listaItemSelecionado, false);
            this.lstItemTotal.DataSource = this.MapperItemToDataTable(this._listaItemsCompleto, true);
            this.VerificarQuantidadeMaximaPermitida();
        }

        private void btnFiltrar_Click(object sender, EventArgs e)
        {
            var itemFiltro = (from item in this._listaItemsCompleto
                              where item.Nome.ToUpper().Contains(this.txtFiltro.Text.ToUpper())
                              select item).ToList();
            this.lstItemTotal.DataSource = this.MapperItemToDataTable(itemFiltro, true);
        }

        private void btnOk_Click(object sender, EventArgs e)
        {
            this.DialogResult = DialogResult.OK;
            ConfiguracaoItemsChampion ownerForm = (ConfiguracaoItemsChampion)this.Owner;
            ownerForm.ListaItem = this._listaItemSelecionado;
            this.Close();
        }

        private void btnCancelar_Click(object sender, EventArgs e)
        {
            this.DialogResult = DialogResult.Cancel;
            this.Close();
        }

    }
}